package com.bjsxt.shopping.user;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.bjsxt.shopping.client.Cart;
import com.bjsxt.shopping.client.CartItem;
import com.bjsxt.shopping.order.OrderMgr;
import com.bjsxt.shopping.order.SalesItem;
import com.bjsxt.shopping.order.SalesOrder;
import com.bjsxt.shopping.util.DB;

public class User {
	private int id;

	private String username;

	private String password;

	private String phone;

	private String addr;

	private String acctype;

	private String status;

	private String translatorLevel;
	
	private String attach;

	public String getTranslatorLevel() {
		return translatorLevel;
	}

	public void setTranslatorLevel(String translatorLevel) {
		this.translatorLevel = translatorLevel;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	private String Tests;

	public String getTests() {
		return Tests;
	}

	public void setTests(String tests) {
		Tests = tests;
	}

	public String getTestsTrans() {
		return TestsTrans;
	}

	public void setTestsTrans(String testsTrans) {
		TestsTrans = testsTrans;
	}

	private String TestsTrans;

	public String getAcctype() {
		return acctype;
	}

	public void setAcctype(String acctype) {
		this.acctype = acctype;
	}

	private Date rdate;

	private String loginname;
	private int translates;
	private int passtimes;

	public String getLoginname() {
		return loginname;
	}

	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}

	public String getAddr() {
		return addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public Date getRdate() {
		return rdate;
	}

	public void setRdate(Date rdate) {
		this.rdate = rdate;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getAttach() {
		return attach;
	}

	public void setAttach(String attach) {
		this.attach = attach;
	}

	public boolean save() throws SQLException {
		Connection conn = DB.getConn();
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String sSaveTime =df.format(new Date());// new Date()为获取当前系统时间
		
		//判断用户名审核未通过 重复
		String sql2 = "select * from userc123 where loginname = '" + username + "' and status in ('2') ";
		Statement stmt2 = DB.getStatement(conn);
		ResultSet rs2 = DB.getResultSet(stmt2, sql2);
		
		if(rs2.next()) {
			String sql3 = " delete from  userc123 where loginname = '" + username + "' and status in ('2')  ";
			PreparedStatement pstmt = DB.prepare(conn, sql3);
			pstmt.executeUpdate();
		}
		
		String sql = "insert into userc123 values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
		PreparedStatement pstmt = DB.prepare(conn, sql);
		try {
			pstmt.setString(1, "");
			pstmt.setString(2, password);
			pstmt.setString(3, phone);
			pstmt.setString(4, "");
			pstmt.setString(5, sSaveTime);
			pstmt.setString(6, username);
			pstmt.setString(7, acctype);
			pstmt.setString(8, status);
			pstmt.setString(9, Tests);
			pstmt.setString(10, TestsTrans);
			pstmt.setString(11, translatorLevel);
			pstmt.setString(12, attach);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			DB.close(pstmt);
			DB.close(conn);
		}
		return true;
	}

	public static List<User> getUsers() {
		List<User> users = new ArrayList<User>();
		Connection conn = DB.getConn();
		String sql = "select * from user";
		Statement stmt = DB.getStatement(conn);
		ResultSet rs = DB.getResultSet(stmt, sql);
		try {
			while (rs.next()) {
				User u = new User();
				u.setId(rs.getInt("id"));
				u.setUsername(rs.getString("username"));
				u.setPassword(rs.getString("password"));
				u.setPhone(rs.getString("phone"));
				u.setAddr(rs.getString("addr"));
				u.setRdate(rs.getTimestamp("rdate"));
				users.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.close(rs);
			DB.close(stmt);
			DB.close(conn);
		}
		return users;
	}

	public static List<User> getWaitAuditTranslators() {
		List<User> users = new ArrayList<User>();
		Connection conn = DB.getConn();
		String sql = " select * from userc123 where acctype ='2' and status = '0' order by rdate  ";
		Statement stmt = DB.getStatement(conn);
		ResultSet rs = DB.getResultSet(stmt, sql);
		try {
			while (rs.next()) {
				User u = new User();
				u.setId(rs.getInt("id"));
				u.setLoginname(rs.getString("loginname"));
				u.setRdate(rs.getTimestamp("rdate"));
				u.setTests(rs.getString("Tests"));
				u.setTestsTrans(rs.getString("TestsTrans"));

				users.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.close(rs);
			DB.close(stmt);
			DB.close(conn);
		}
		return users;
	}

	/**
	 * 
	 * @param users
	 * @param pageNo
	 * @param pageSize
	 * @return �ܹ��ж�������¼
	 */
	public static int getUsers(List<User> users, int pageNo, int pageSize) {

		int totalRecords = -1;

		Connection conn = DB.getConn();
		String sql = "select * from user limit " + (pageNo - 1) * pageSize
				+ "," + pageSize;
		Statement stmt = DB.getStatement(conn);
		ResultSet rs = DB.getResultSet(stmt, sql);

		Statement stmtCount = DB.getStatement(conn);
		ResultSet rsCount = DB.getResultSet(stmtCount,
				"select count(*) from user");

		try {
			rsCount.next();
			totalRecords = rsCount.getInt(1);

			while (rs.next()) {
				User u = new User();
				u.setId(rs.getInt("id"));
				u.setUsername(rs.getString("username"));
				u.setPassword(rs.getString("password"));
				u.setPhone(rs.getString("phone"));
				u.setAddr(rs.getString("addr"));
				u.setRdate(rs.getTimestamp("rdate"));
				users.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.close(rsCount);
			DB.close(stmtCount);
			DB.close(rs);
			DB.close(stmt);
			DB.close(conn);
		}

		return totalRecords;
	}

	public static boolean delete(int id) {
		boolean b = false;
		Connection conn = DB.getConn();
		String sql = "delete from user where id = " + id;
		Statement stmt = DB.getStatement(conn);

		try {
			DB.executeUpdate(stmt, sql);
			b = true;
		} finally {
			DB.close(stmt);
			DB.close(conn);
		}
		return b;
	}

	public static User check(String username, String password)
			throws UserNotFoundException, PasswordNotCorrectException {
		User u = null;
		Connection conn = DB.getConn();
		String sql = "select * from userc123 where loginname = '"
				+ username.trim() + "'";
		Statement stmt = DB.getStatement(conn);
		ResultSet rs = DB.getResultSet(stmt, sql);
		try {
			if (!rs.next()) {
				throw new UserNotFoundException("对不起，该用户名不存在！"
						+ username.trim());
			} else {
				if (!password.equals(rs.getString("password"))) {
					throw new PasswordNotCorrectException("对不起，密码不正确!");
				}
				u = new User();
				u.setId(rs.getInt("id"));
				u.setUsername(rs.getString("username"));
				u.setPassword(rs.getString("password"));
				u.setPhone(rs.getString("phone"));
				u.setAddr(rs.getString("addr"));
				u.setRdate(rs.getTimestamp("rdate"));
				u.setLoginname(rs.getString("loginname"));
				u.setAcctype(rs.getString("acctype"));
				u.setStatus(rs.getString("status"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.close(rs);
			DB.close(stmt);
			DB.close(conn);
		}
		return u;
	}

	public static boolean checkUserNotExist(String username)
			throws UserNotFoundException, PasswordNotCorrectException {
		User u = null;
		boolean retBl = true;
		Connection conn = DB.getConn();
		String sql = "select * from userc123 where loginname = '" + username
				+ "'";

		Statement stmt = DB.getStatement(conn);
		ResultSet rs = DB.getResultSet(stmt, sql);
		try {
			if (rs.next()) {
				retBl = false;
			} else {
				retBl = true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.close(rs);
			DB.close(stmt);
			DB.close(conn);
		}
		return retBl;
	}

	public static boolean checkUserTransorNotExist(String username)
			throws UserNotFoundException, PasswordNotCorrectException {
		User u = null;
		boolean retBl = true;
		Connection conn = DB.getConn();
		String sql = "select * from userc123 where loginname = '" + username
				+ "' and status in ('0','1') ";

		Statement stmt = DB.getStatement(conn);
		ResultSet rs = DB.getResultSet(stmt, sql);

		try {
			if (rs.next()) {
				retBl = false;
			} else {
				retBl = true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.close(rs);
			DB.close(stmt);
			DB.close(conn);
		}
		return retBl;
	}

	public static void update(User u) {
		u.update();
	}

	public void updatePassword() {

	}

	public static void updatePassword(User u) {
		u.updatePassword();
	}

	public static void updatePassword(int userId, String newPassword) {

	}

	public static void updateStatus(String loginname, String newStatus) {
		Connection conn = DB.getConn();
		String sql = "update userc123 set status = ?  where loginname = ?";
		PreparedStatement pstmt = DB.prepare(conn, sql);
		try {
			pstmt.setString(1, newStatus);
			pstmt.setString(2, loginname);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.close(pstmt);
			DB.close(conn);
		}
	}

	public void update() {
		Connection conn = DB.getConn();
		String sql = "update user set phone = ?, addr = ? where id = ?";
		PreparedStatement pstmt = DB.prepare(conn, sql);
		try {
			pstmt.setString(1, phone);
			pstmt.setString(2, addr);
			pstmt.setInt(3, id);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.close(pstmt);
			DB.close(conn);
		}
	}

	public static void main(String[] args) {
		List<User> users = new ArrayList<User>();
		int totalRecords = User.getUsers(users, 1, 10);
		for (int i = 0; i < users.size(); i++) {

		}
	}

	public int buy(Cart c) {
		SalesOrder so = new SalesOrder();
		so.setUser(this);
		so.setAddr(this.getAddr());
		so.setStatus(0);
		so.setODate(new Date());
		List<SalesItem> salesItems = new ArrayList<SalesItem>();
		List<CartItem> cartItems = c.getItems();
		for (int i = 0; i < cartItems.size(); i++) {
			SalesItem si = new SalesItem();
			CartItem ci = cartItems.get(i);
			si.setProduct(ci.getProduct());
			si.setCount(ci.getCount());
			// si.setUnitPrice(ci.getProduct().getMemberPrice());
			salesItems.add(si);
		}
		so.setItems(salesItems);
		return OrderMgr.getInstance().add(so);
	}

	public int getPasstimes() {
		return passtimes;
	}

	public void setPasstimes(int passtimes) {
		this.passtimes = passtimes;
	}

	public int getTranslates() {
		return translates;
	}

	public void setTranslates(int translates) {
		this.translates = translates;
	}

	
}
